define(['jquery', 'bootstrap', 'userend', 'table', 'form'], function ($, undefined, Userend, Table, Form) {
    var Controller = {
        index: function () {
            var params = '/freight_id/' + Config.freight_id;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/shop/freightprice/index' + location.search,
                    add_url: 'mall/shop/freightprice/add' + params,
                    edit_url: 'mall/shop/freightprice/edit',
                    del_url: 'mall/shop/freightprice/del',
                    multi_url: 'mall/shop/freightprice/multi',
                    table: 'mall_shop_freightprice',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'rule_id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'rule_id', title: __('ID'), visible:false},
                        {field: 'freight_id_text', title: __('Freight_id')},
                        {field: 'express_ids_text', title: __('Express_ids')},
                        {field: 'region_ids_text', title: __('Region_ids')},
                        {field: 'first', title: __('First'), operate:'BETWEEN'},
                        {field: 'first_fee', title: __('First_fee'), operate:'BETWEEN'},
                        {field: 'additional', title: __('Additional'), operate:'BETWEEN'},
                        {field: 'additional_fee', title: __('Additional_fee'), operate:'BETWEEN'},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $("#c-category1").trigger("fa.event.areaidsupdated",'edit');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //四级地区联动参数传递
                $("#c-category2").data("params", function (obj) {
                    return {custom: {pid: ['in',$("#c-category1").val()]}};
                });
                //一级地区选定，清除二级选项
                $(document).on("change", "#c-category1", function () {
                    require(['selectpage'], function () {
                        $('#c-category2').selectPageClear();
                    });
                });
                //拼接二级地区选项值，用于保存
                $(document).on("change", "#c-category2", function () {
                    $("#c-region_ids").val( $("#c-category1").val()+","+$(this).val());
                });
                //四级地区默认值设置（只执行一次）
                $(document).one("fa.event.areaidsupdated", "#c-category1", function () {
                    var a = $("#c-region_ids").val().split(",");
                    $("#c-category1").val(a[0]);
                    $("#c-category2").val(a[1]);
                });
            }
        }
    };
    return Controller;
});